package br.com.fatesg.www.controle;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.fatesg.www.dao.DAO;
import br.com.fatesg.www.dao.IndustriaDAO;
import br.com.fatesg.www.dao.ResiduoDAO;
import br.com.fatesg.www.entidade.Industria;
import br.com.fatesg.www.entidade.Residuo;
import br.com.fatesg.www.util.ExcecaoInfraEstrutura;
import br.com.fatesg.www.util.HibernateUtil;

public class ResiduoSV extends HttpServlet {

	private static final long serialVersionUID = -1825179710786379943L;

	DAO<Residuo, Long> dao = new ResiduoDAO(HibernateUtil.getSessionFactory().openSession());
	DAO<Industria, Long> industiaDao = new IndustriaDAO(HibernateUtil.getSessionFactory().openSession());

	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String acao = request.getParameter("acao");

			Residuo residuo;

			residuo = new Residuo();

			Long id = (request.getParameter("id") == "" || request.getParameter("id") == null) ? 0 : Long.parseLong(request.getParameter("id"));
			if (id > 0)
				residuo.setId(id);

			if (acao.equals("novo")) {
				request.setAttribute("residuo", new Residuo());
				request.getRequestDispatcher("frmResiduo.jsp").forward(request, response);
			}
			if (acao.equals("editar")) {
				Residuo residuoObtido = dao.obterPeloId(Residuo.class, id);
				request.setAttribute("residuo", residuoObtido);
				request.getRequestDispatcher("frmResiduo.jsp").forward(request, response);
			}
			if (acao.equals("inserir")) {

				residuo.setDescricao(request.getParameter("txtDescricao"));
				residuo.setOperacao(Integer.parseInt(request.getParameter("txtOperacao")));
				residuo.setQuantidade(Integer.parseInt(request.getParameter("txtQuantidade")));
				residuo.setTipo(request.getParameter("txtTipo"));
				residuo.setValor(Double.parseDouble(request.getParameter("txtValor")));
				residuo.setIndustria(industiaDao.obter(Long.parseLong(request.getParameter("txtCnpj"))));

				if (id != null) {
					dao.atualizar(residuo);
				} else
					dao.salvar(residuo);

				request.setAttribute("lstResiduo", dao.listar(Residuo.class));
				request.getRequestDispatcher("lstResiduo.jsp").forward(request, response);
			} else if (acao.equals("atualizar")) {
				dao.atualizar(residuo);
				request.setAttribute("lstResiduo", dao.listar(Residuo.class));
				request.getRequestDispatcher("lstResiduo.jsp").forward(request, response);
			} else if (acao.equals("excluir")) {

				dao.excluir(dao.obterPeloId(Industria.class, id));
				request.setAttribute("lstResiduo", dao.listar(Residuo.class));
				request.getRequestDispatcher("lstResiduo.jsp").forward(request, response);
			} else if (acao.equals("obter")) {
				dao.obter(id);

			} else if (acao.equals("listar")) {
				request.setAttribute("lstResiduo", dao.listar(Residuo.class));
				request.getRequestDispatcher("lstResiduo.jsp").forward(request, response);
			}
		} catch (ExcecaoInfraEstrutura ex) {
			dao.getSession().getTransaction().rollback();
			dao.getSession().close();
			request.setAttribute("msg", "Execaoo: " + ex.getMessage());
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}

}
